package edu.xlh.travel.echart;

import edu.xlh.travel.asynctask.AsyncTask;
import edu.xlh.travel.common.EchartInRedisConstant;
import edu.xlh.travel.redis.utils.JedisManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class EchartTest {

    @Autowired
    private JedisManager jedisManager;
    @Autowired
    private AsyncTask asyncTask;

    private String operatedKey = EchartInRedisConstant.ECHART_RD_DATA+"::"
            +EchartInRedisConstant.DATACOUNTWRITE_RD_LIST+"::"
            +EchartInRedisConstant.OPERATED_RD_DATE;

    @Autowired
    public void init() {
        System.out.println("initMethod having excute....");
        String nowDateToString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if (!jedisManager.exists(operatedKey)) {
            jedisManager.set(operatedKey , nowDateToString);
        }
        //获取数据库的操作标志，值为时间，与当天年月日比较，相同则说明已经统计过了，一天只能统计一次，
        // 后期可以设计为在当天某个时间段统计
        String  redisDate = jedisManager.get(operatedKey);
        if (!nowDateToString.equals(redisDate)) {
            //调用异步函数统计.....
            asyncTask.doHandleWriteReportDataToRedis();
        }
        //当天统计完成后修改操作标志为当天的年月日
        jedisManager.set(operatedKey , nowDateToString);
        System.out.println("initMethod having excute end....");
    }
}
